package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.service.MenuService;
import com.itheima.service.PermissionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Reference
    private PermissionService permissionService;

    @RequestMapping("/findAll")
    //查询所有
    public Result findAll() {
        //调用service返回checkItem list集合
        List<Permission> PermissionList = permissionService.findAll();
        if (PermissionList != null && PermissionList.size() > 0) {
            Result result = new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS);
            result.setData(PermissionList);
            return result;
        } else {
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

//    //设置修改权限
//    @PreAuthorize("hasAnyAuthority('USER_QUERY')")
    @RequestMapping("/findPage")
    //分页查询
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        //调用分页查询方法
        PageResult pageResult = permissionService.pageQuery(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
        //返回PageResult
        return pageResult;
    }

//    //设置权限
//    @PreAuthorize("hasAnyAuthority('USER_ADD')")
    //添加
    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission) {
        //try
        try {
            //调用add方法
            permissionService.add(permission);
            //返回添加成功
            return new Result(true, MessageConstant.ADD_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //返回添加失败
            return new Result(false, MessageConstant.ADD_PERMISSION_FAIL);
        }
    }

    //添加编辑权限
//    @PreAuthorize("hasAnyAuthority('USER_EDIT')")
    @RequestMapping("/edit")
    //编辑
    public Result edit(@RequestBody Permission permission) {
        //try
        try {
            //调用编辑方法
            permissionService.edit(permission);
            //返回编辑成功
            return new Result(true, MessageConstant.EDIT_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //返回编辑失败
            return new Result(false, MessageConstant.EDIT_PERMISSION_FAIL);
        }
    }

    //设置修改权限
//    @PreAuthorize("hasAnyAuthority('USER_QUERY')")
    @RequestMapping("/findById")
    //通过id查询
    public Result findById(Integer id) {
        //调用id查询方法
        Permission permission = permissionService.findById(id);
        //判断数据是否为空
        if (permission != null) {
            //数据不为空新建查询成功Result
            Result result = new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS);
            //result调用setData方法吧查询结果添加进result
            result.setData(permission);
            //返回result
            return result;
        } else {
            //返回查询失败
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

//    @PreAuthorize("hasAnyAuthority('USER_DELETE')")
    @RequestMapping("/delete")
    //删除
    public Result delete(Integer id) {
        //try
        try {
            //调用删除方法
            permissionService.delete(id);
        } catch (RuntimeException e) {
            //catch自定义错误并返回
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            //返回删除失败
            return new Result(false, MessageConstant.DELETE_PERMISSION_FAIL);
        }
        //返回删除成功
        return new Result(true, MessageConstant.DELETE_PERMISSION_SUCCESS);
    }
}
